package edu.ncsu.contractminer.event;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import edu.ncsu.contractminer.utilities.Constants;

public class EventClassifierTrainingSetForWeka {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			FileWriter fw = new FileWriter(Constants.wekaFile);
			BufferedWriter bw = new BufferedWriter(fw);
			StringBuilder mNominal = new StringBuilder();
			StringBuilder cNominal = new StringBuilder();

			mNominal.append("{");
			cNominal.append("{");
			for (int i = 0; i < StringToNumericForWeka.getModalVerNominalSize(); i++)
				mNominal.append(i + ",");
			mNominal.append("}");
			for (int i = 0; i < StringToNumericForWeka
					.getClauseSignalNorminalSize(); i++)
				cNominal.append(i + ",");
			cNominal.append("}");

			bw.write("@RELATION	" + Constants.eventTableName + "\n"
					+ "@ATTRIBUTE	SubContainsNE	{1,0}\n"
					+ "@ATTRIBUTE	ModalVerb	" + mNominal.toString() + "\n"
					+ "@ATTRIBUTE	ClauseSignal	" + cNominal.toString() + "\n"
					+ "@ATTRIBUTE	CounterClauseSignal " + cNominal.toString()
					+ "\n" + "@ATTRIBUTE	class	{1,0}\n" + "@DATA\n");

			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			String dataSourceName = "NormDB";
			String dbURL = "jdbc:odbc:" + dataSourceName;
			Connection con = DriverManager.getConnection(dbURL, "", "");
			// try and create a java.sql.Statement so we can run queries
			Statement statement = con.createStatement();

			String query1 = "SELECT IsEvent,SubContainsNE,ModalVerb,ClauseSignal,CounterClauseSignal FROM "
					+ Constants.eventTableName;
			statement.execute(query1);
			ResultSet result = statement.getResultSet();
			if (result != null) {
				while (result.next()) {
					boolean isEvent = result.getBoolean("IsEvent");
					boolean subContainsNE = result.getBoolean("SubContainsNE");
					String modalVerb = result.getString("ModalVerb")
							.toLowerCase();
					String clauseSignal = result.getString("ClauseSignal")
							.toLowerCase();
					String counterClauseSignal = result.getString(
							"CounterClauseSignal").toLowerCase();
					bw
							.write((int) (subContainsNE ? 1 : 0)
									+ ","
									+ StringToNumericForWeka
											.modalVerbStringToNumerical(modalVerb)
									+ ","
									+ StringToNumericForWeka
											.subClauseWordStringToNumerical(clauseSignal)
									+ ","
									+ StringToNumericForWeka
											.subClauseWordStringToNumerical(counterClauseSignal)
									+ "," + (int) (isEvent ? 1 : 0));
					bw.newLine();

				}
			}
			bw.flush();
			bw.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
